home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Obj Hierarchy ƒ / ObjectHierarchySample.c next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  4.0 KB  |  138 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include "QD3D.h"
  6.  
  7. /*
  8.  * Quick Hack to dump the object hierarchy for QuickDraw 3D.
  9.  *
  10.  * This is a neat little snippet that shows how to use several
  11.  * of the new object system calls (check out QD3D.h for the prototypes):
  12.  *
  13.  *
  14.  *    Q3ObjectHierarchy_GetStringFromType
  15.  *    -----------------------------------    
  16.  *
  17.  *   TQ3Status Q3ObjectHierarchy_GetStringFromType(
  18.  *      TQ3ObjectType                objectClassType, 
  19.  *      TQ3ObjectClassNameString     objectClassString);
  20.  *  
  21.  *  Given a class type as return the associated string for the class name, 
  22.  *  may return kQ3Failure if the type representing the class is invalid.
  23.  *
  24.  *
  25.  *     struct TQ3SubClassData
  26.  *    ----------------------
  27.  *    typedef struct TQ3SubClassData {
  28.  *        unsigned long         numClasses;        -- the # of subclass types found
  29.  *                                            -- for a parent class
  30.  *
  31.  *        TQ3ObjectType        *classTypes;    -- an array containing the class
  32.  *                                            -- types                        
  33.  *    } TQ3SubClassData;
  34.  *     
  35.  *     TQ3SubClassData is used when querying the object system for
  36.  *     the subclasses of a particular parent type, it is allocated 
  37.  *    with a call to Q3ObjectClass_GetSubClassData, so every instance
  38.  *    of this call must be matched with a call to Q3ObjectClass_EmptySubClassData
  39.  *    or your application will leak.
  40.  *
  41.  *
  42.  *    Q3ObjectClass_GetSubClassData
  43.  *    -----------------------------
  44.  *
  45.  *    TQ3Status QD3D_CALL Q3ObjectClass_GetSubClassData(
  46.  *        TQ3ObjectType                objectClassType, 
  47.  *        TQ3SubClassData                *subClassData);
  48.  *
  49.  *  Given a parent type and an instance of the TQ3SubClassData struct fill
  50.  *  it in with the number and class types of all of the subclasses immediately
  51.  *  below the parent in the class hierarchy.  Return kQ3Success to indicate no
  52.  *  errors occurred, else kQ3Failure.
  53.  *
  54.  *  NOTE:  This function will allocate memory for the classTypes array.  Be 
  55.  *    sure to call Q3ObjectClass_EmptySubClassData to free this memory up.
  56.  *
  57.  *
  58.  *    Q3ObjectClass_EmptySubClassData
  59.  *  -------------------------------
  60.  *
  61.  *  TQ3Status  Q3ObjectClass_EmptySubClassData(
  62.  *        TQ3SubClassData                *subClassData );
  63.  *
  64.  *  Given an instance of the TQ3SubClassData struct free all memory allocated 
  65.  *    by the Q3ObjectClass_GetSubClassData call.
  66.  *
  67.  *  NOTE: This call MUST be made after a call to Q3ObjectClass_GetSubClassData
  68.  *  to avoid memory leaks.
  69.  *
  70.  *    We just use metrowerks console library to dump out a text list of the
  71.  *    hierarchy, with the appropriate level of indentation.
  72.  *
  73.  * Nick Thompson, nickt@apple.com, 10/96
  74.  *
  75.  */
  76.  
  77. /* prototypes  */
  78. static void PrintClassAndRecurse( TQ3ObjectType objectClassType, int depth ) ;
  79.  
  80. /* entry point */
  81. void main(void)
  82. {
  83.     printf ("QuickDraw™ 3D Object Hierarchy\n\n");
  84.  
  85.     Q3Initialize() ;
  86.     
  87.     /*
  88.      * The class "Object" is in fact a virtual base class, it is not possible
  89.      * to instantiate this class.  At the root of the hierarchy are four classes
  90.      * these are:
  91.      *    View
  92.      *    Pick
  93.      *    Element
  94.      *    Shared
  95.      * So we can fake out object at the root of the hierarchy, and go from each
  96.      * of the four named classes.
  97.      */
  98.     
  99.     printf( "Root Object (virtual metaclass)\n" ) ;
  100.     
  101.     PrintClassAndRecurse( kQ3ObjectTypeView, 0 ) ;
  102.     PrintClassAndRecurse( kQ3ObjectTypeElement, 0 ) ;
  103.     PrintClassAndRecurse( kQ3ObjectTypePick, 0 ) ;
  104.     PrintClassAndRecurse( kQ3ObjectTypeShared, 0 ) ;
  105.     
  106.     Q3Exit() ;
  107. }
  108.  
  109. /* the guts... :) */
  110. static void PrintClassAndRecurse( TQ3ObjectType objectClassType, int depth )
  111. {
  112.     TQ3SubClassData                mySubClassData ;
  113.     TQ3ObjectClassNameString     myObjectClassString ;
  114.     unsigned long                index ;
  115.     
  116.     depth++ ;
  117.     if( objectClassType != kQ3ObjectTypeInvalid )
  118.     {    
  119.         
  120.         Q3ObjectHierarchy_GetStringFromType( objectClassType, myObjectClassString) ;
  121.         
  122.         for(index = 0; index < depth; index++)
  123.             printf(" ") ;
  124.         
  125.         printf("%s\n", myObjectClassString) ;
  126.         
  127.         Q3ObjectHierarchy_GetSubClassData( objectClassType, &mySubClassData) ;
  128.         
  129.         for( index = 0; index < mySubClassData.numClasses; index++ )
  130.         {
  131.             /* recurse on each subclass type */
  132.             PrintClassAndRecurse( mySubClassData.classTypes[index], depth ) ;
  133.         }
  134.         
  135.         Q3ObjectHierarchy_EmptySubClassData( &mySubClassData ) ;
  136.     }
  137.     depth-- ;
  138. }